package cn.edu.sgu.www.mhxysy.service.chongwu.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuInitZizhiDTO;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuInitZizhiOrdinaryDTO;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuInitZizhiSpecialDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuCategory;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuInitZizhi;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuInitZizhiMapper;
import cn.edu.sgu.www.mhxysy.pager.chongwu.ChongwuInitZizhiPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuInitZizhiService;
import cn.edu.sgu.www.mhxysy.util.CollectionUtils;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuInitZizhiServiceImpl implements ChongwuInitZizhiService {

	private final ChongwuCategoryMapper chongwuCategoryMapper;
	private final ChongwuInitZizhiMapper chongwuInitZizhiMapper;

	@Autowired
	public ChongwuInitZizhiServiceImpl(
			ChongwuCategoryMapper chongwuCategoryMapper,
			ChongwuInitZizhiMapper chongwuInitZizhiMapper) {
		this.chongwuCategoryMapper = chongwuCategoryMapper;
		this.chongwuInitZizhiMapper = chongwuInitZizhiMapper;
	}

	@Override
	public void insert(ChongwuInitZizhiDTO chongwuInitZizhiDTO) {
		ChongwuInitZizhi chongwuInitZizhi = toEntity(chongwuInitZizhiDTO);

		chongwuInitZizhi.setId(null);

		chongwuInitZizhiMapper.insert(chongwuInitZizhi);
	}

	@Override
	public void deleteById(Integer id) {
		chongwuInitZizhiMapper.deleteById(id);
	}

	@Override
	public void updateById(ChongwuInitZizhiDTO chongwuInitZizhiDTO) {
		if (chongwuInitZizhiDTO.getId() == null) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "ID不能为空！");
		}

		ChongwuInitZizhi chongwuInitZizhi = toEntity(chongwuInitZizhiDTO);

		chongwuInitZizhiMapper.updateById(chongwuInitZizhi);
	}

	@Override
	public void save(ChongwuInitZizhiOrdinaryDTO chongwuInitZizhiOrdinaryDTO) {
		// 根据类型和宠物类型ID查询
		List<ChongwuInitZizhi> list = selectList(chongwuInitZizhiOrdinaryDTO.getType(), chongwuInitZizhiOrdinaryDTO.getChongwuId());

		if (CollectionUtils.isEmpty(list)) {
			// 普通宝宝
			if (chongwuInitZizhiOrdinaryDTO.getType() == 1) {
				// 添加普通资质
				ChongwuInitZizhi chushiZizhi = new ChongwuInitZizhi();

				chushiZizhi.setId(null);
				chushiZizhi.setType(1);
				chushiZizhi.setGjzz(chongwuInitZizhiOrdinaryDTO.getGjzz());
				chushiZizhi.setFyzz(chongwuInitZizhiOrdinaryDTO.getFyzz());
				chushiZizhi.setTlzz(chongwuInitZizhiOrdinaryDTO.getTlzz());
				chushiZizhi.setFlzz(chongwuInitZizhiOrdinaryDTO.getFlzz());
				chushiZizhi.setSdzz(chongwuInitZizhiOrdinaryDTO.getSdzz());
				chushiZizhi.setChongwuId(chongwuInitZizhiOrdinaryDTO.getChongwuId());
				chushiZizhi.setChengzhang(chongwuInitZizhiOrdinaryDTO.getChengzhang());

				chongwuInitZizhiMapper.insert(chushiZizhi);

				// 得到宠物资质信息
				int gjzz = chushiZizhi.getGjzz();
				int fyzz = chushiZizhi.getFyzz();
				int tlzz = chushiZizhi.getTlzz();
				int flzz = chushiZizhi.getFlzz();
				int sdzz = chushiZizhi.getSdzz();
				int chongwuId = chushiZizhi.getChongwuId();
				int chengzhang = chushiZizhi.getChengzhang();

				// 野生资质（野生 = 普通 * 0.8）
				ChongwuInitZizhi ys = new ChongwuInitZizhi();

				ys.setType(0);
				ys.setGjzz((int) (gjzz * 0.8));
				ys.setFyzz((int) (fyzz * 0.8));
				ys.setTlzz((int) (tlzz * 0.8));
				ys.setFlzz((int) (flzz * 0.8));
				ys.setSdzz((int) (sdzz * 0.8));
				ys.setChongwuId(chongwuId);
				ys.setChengzhang(chengzhang - 36);

				chongwuInitZizhiMapper.insert(ys);

				// 变异资质（变异 = 普通 * 1.08）
				ChongwuInitZizhi by = new ChongwuInitZizhi();

				by.setType(2);
				by.setGjzz((int) (gjzz * 1.08));
				by.setFyzz((int) (fyzz * 1.08));
				by.setTlzz((int) (tlzz * 1.08));
				by.setFlzz((int) (flzz * 1.08));
				by.setSdzz((int) (sdzz * 1.08));
				by.setChongwuId(chongwuId);
				by.setChengzhang(chengzhang);

				chongwuInitZizhiMapper.insert(by);
			} else {
				throw new GlobalException(ResponseCode.FORBIDDEN, "暂时只支持添加普通宝宝的资质。");
			}
		}
	}

	@Override
	public void saveSpecial(ChongwuInitZizhiSpecialDTO chongwuInitZizhiSpecialDTO) {
		Integer chongwuId = chongwuInitZizhiSpecialDTO.getChongwuId();

		/*
		 * 神兽的初始资质不可修改
		 * 神兽的初始资质，宠物类型ID字段的值为null
		 */
		if (chongwuId == null) {
			throw new GlobalException(ResponseCode.CONFLICT, "神兽的资质不可修改！");
		}

		// 查询宠物类型信息
		ChongwuCategory chongwuCategory = chongwuCategoryMapper.selectById(chongwuId);
		// 特殊宠物的资质类型
		Integer specialType = chongwuCategory.getSpecialType();

		if (specialType != null) {
			// 查询宠物资质信息
			QueryWrapper<ChongwuInitZizhi> queryWrapper = new QueryWrapper<>();

			queryWrapper.eq("special_type", specialType);
			queryWrapper.eq("improve_times", chongwuInitZizhiSpecialDTO.getImproveTimes());

			List<ChongwuInitZizhi> list = chongwuInitZizhiMapper.selectList(queryWrapper);

			if (list.size() > 1) {
				throw new GlobalException(ResponseCode.BAD_REQUEST, "数据异常，出现重复数据！");
			}

			ChongwuInitZizhi chongwuChushiZizhi = new ChongwuInitZizhi();

			chongwuChushiZizhi.setGjzz(chongwuInitZizhiSpecialDTO.getGjzz());
			chongwuChushiZizhi.setFyzz(chongwuInitZizhiSpecialDTO.getFyzz());
			chongwuChushiZizhi.setTlzz(chongwuInitZizhiSpecialDTO.getTlzz());
			chongwuChushiZizhi.setFlzz(chongwuInitZizhiSpecialDTO.getFlzz());
			chongwuChushiZizhi.setSdzz(chongwuInitZizhiSpecialDTO.getSdzz());
			chongwuChushiZizhi.setChengzhang(chongwuInitZizhiSpecialDTO.getChengzhang());

			chongwuChushiZizhi.setName(chongwuInitZizhiSpecialDTO.getName());
			chongwuChushiZizhi.setFightGrade(chongwuInitZizhiSpecialDTO.getFightGrade());
			chongwuChushiZizhi.setSpecialType(specialType);
			chongwuChushiZizhi.setImproveTimes(chongwuInitZizhiSpecialDTO.getImproveTimes());
			chongwuChushiZizhi.setSkillQuantity(chongwuInitZizhiSpecialDTO.getSkillQuantity());
			chongwuChushiZizhi.setNeidanQuantity(chongwuInitZizhiSpecialDTO.getNeidanQuantity());

			if (list.isEmpty()) {
				chongwuInitZizhiMapper.insert(chongwuChushiZizhi);
			} else if (list.size() == 1) {
				chongwuChushiZizhi.setId(list.get(0).getId());

				chongwuInitZizhiMapper.updateById(chongwuChushiZizhi);
			}
		} else {
			throw new GlobalException(ResponseCode.CONFLICT, "普通宠物的资质不是通过这个接口保存！");
		}
	}

	@Override
	public List<ChongwuInitZizhi> selectSpecials(Integer specialType) {
		QueryWrapper<ChongwuInitZizhi> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("special_type", specialType);

		return chongwuInitZizhiMapper.selectList(queryWrapper);
	}

	@Override
	public List<ChongwuInitZizhi> selectList(Integer type, Integer chongwuId) {
		QueryWrapper<ChongwuInitZizhi> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq(type != null, "type", type);
		queryWrapper.eq("chongwu_id", chongwuId);

		queryWrapper.orderByAsc("type");

		return chongwuInitZizhiMapper.selectList(queryWrapper);
	}

	@Override
	public Page<ChongwuInitZizhi> selectByPage(ChongwuInitZizhiPager pager) {
		QueryWrapper<ChongwuInitZizhi> queryWrapper = new QueryWrapper<>();
		Page<ChongwuInitZizhi> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getChongwuId() != null,
				"chongwu_id", pager.getChongwuId()
		);
		queryWrapper.eq(
				pager.getSpecialType() != null,
				"special_type", pager.getSpecialType()
		);

		return chongwuInitZizhiMapper.selectPage(page, queryWrapper);
	}

	private ChongwuInitZizhi toEntity(ChongwuInitZizhiDTO chongwuInitZizhiDTO) {
		ChongwuInitZizhi chongwuInitZizhi = new ChongwuInitZizhi();

		chongwuInitZizhi.setId(chongwuInitZizhiDTO.getId());
		chongwuInitZizhi.setGjzz(chongwuInitZizhiDTO.getGjzz());
		chongwuInitZizhi.setFyzz(chongwuInitZizhiDTO.getFyzz());
		chongwuInitZizhi.setTlzz(chongwuInitZizhiDTO.getTlzz());
		chongwuInitZizhi.setFlzz(chongwuInitZizhiDTO.getFlzz());
		chongwuInitZizhi.setSdzz(chongwuInitZizhiDTO.getSdzz());
		chongwuInitZizhi.setChongwuId(chongwuInitZizhiDTO.getChongwuId());
		chongwuInitZizhi.setChengzhang(chongwuInitZizhiDTO.getChengzhang());

		if (chongwuInitZizhiDTO.getType() != null) { // 普通、稀有宝宝
			chongwuInitZizhi.setName(null);
			chongwuInitZizhi.setFightGrade(null);
			chongwuInitZizhi.setSpecialType(null);
			chongwuInitZizhi.setImproveTimes(null);
			chongwuInitZizhi.setSkillQuantity(null);
			chongwuInitZizhi.setNeidanQuantity(null);

			chongwuInitZizhi.setType(chongwuInitZizhiDTO.getType());
		} else { // 特殊宝宝（限定、神兽、珍兽、灵兽）
			chongwuInitZizhi.setType(null);

			// 校验6个字段的值
			String name = ObjectUtils.requireNonNull(chongwuInitZizhiDTO.getName(), "资质名称不允许为空！");
			Integer fightGrade = ObjectUtils.requireNonNull(chongwuInitZizhiDTO.getFightGrade(), "参战等级不允许为空！");
			Integer specialType = ObjectUtils.requireNonNull(chongwuInitZizhiDTO.getSpecialType(), "特殊类型不允许为空！");
			Integer improveTimes = ObjectUtils.requireNonNull(chongwuInitZizhiDTO.getImproveTimes(), "提升次数不允许为空！");
			Integer skillQuantity = ObjectUtils.requireNonNull(chongwuInitZizhiDTO.getSkillQuantity(), "技能数量不允许为空！");
			Integer neidanQuantity = ObjectUtils.requireNonNull(chongwuInitZizhiDTO.getNeidanQuantity(), "内丹数量不允许为空！");

			chongwuInitZizhi.setName(name);
			chongwuInitZizhi.setFightGrade(fightGrade);
			chongwuInitZizhi.setSpecialType(specialType);
			chongwuInitZizhi.setImproveTimes(improveTimes);
			chongwuInitZizhi.setSkillQuantity(skillQuantity);
			chongwuInitZizhi.setNeidanQuantity(neidanQuantity);
		}
		
		return chongwuInitZizhi;
	}

}